<template>
	<view class="main" :style="{'background-image': 'url(' + bgUrl + ')'}">
		<button v-if="!auth.phone && auth.id" style="width: 100%;margin-top: 50%;" open-type="getPhoneNumber"
			@getphonenumber="onGetPhoneNumber">获取手机号</button>
		<!-- 2022-04 已弃用 <button style="width: 100%;margin-top: 10%;" @click="onGetUserProfile">获取用户基本信息</button> -->
		<button style="width: 100%;margin-top: 10%;" @click="onClickGo">go</button>
	</view>
</template>

<script setup>
	const title = '登录'
	const bgUrl = 'https://ygsab.ygteacher.com/storage/images/login-bg1.jpg'

	import {
		useAuthStore
	} from '@/pages_temp/store/auth.js'

	import {
		login,
		updateUserPhone,
		updateUserFrom
	} from '@/pages_temp/api/auth.js'

	import tools from "@/pages_temp/utils/tools.js"

	const auth = useAuthStore()
	const props = defineProps({
		from: String
	})
	console.log('props: ', props);

	tools.login().then((res) => {
		if (res) {
			login(res.code).then(res => {
				tools.setToken(res.openid)
				console.log('res : ', res);
				init(res)
			}).catch(e => {
				console.log('res:', e);
			})
		} else {
			init()
		}
	})
	const init = (user) => {
		auth.setUser(user).then(res => {
			console.log('res: ', res);
			console.log('id: ', auth.id);
			if (!auth.from && props.from) {
				updateUserFrom(props.from).then(res => {
					console.log(res);
					auth.setUser(res)
				})
			}
		})
	}

	//调用条件： uni.login 并且为企业认证小程序
	const onGetPhoneNumber = (res) => {
		console.log(res);
		if (res.detail.errMsg == 'getPhoneNumber:ok') {
			console.log('同意获取手机号');
			console.log('data: ', res.detail);
			updateUserPhone(res.detail.code).then(res => {
				auth.setUser(res)
			})
		}
	}
	// 2022-04 已弃用
	// const onGetUserProfile = (res) => {
	// 	uni.getUserProfile({
	// 		desc: '测试！！',
	// 		success: (res) => {
	// 			console.log(res);
	// 			if (res.errMsg == "getUserProfile:ok") {
	// 				console.log('同意获取基本信息', JSON.parse(res.rawData));
	// 			}
	// 		},
	// 		fail: (e) => {
	// 			console.log(e);
	// 		}
	// 	})
	// }
	const onClickGo = () => {
		uni.reLaunch({
			url: "/pages_temp/views/layout"
		})
	}
</script>

<style lang="scss" scoped>
	.main {
		background-size: 100% 100%;
		background-repeat: no-repeat;
		flex-direction: column;
		position: absolute;
		bottom: 0;
		top: 0;
		right: 0;
		left: 0;
	}
</style>
